//
//  TCLYEvaluationViewController.m
//  YuErmuYing
//
//  Created by LYang on 16/8/19.
//  Copyright © 2016年 Tiancheng. All rights reserved.
//


#import "TCLYEvaluationViewController.h"
#import "TCLYEvaluationView.h"
#import "ZLPhotoActionSheet.h"
#import "ZLShowBigImage.h"
#import "ZLDefine.h"
#import "ZLCollectionCell.h"
#import "Masonry.h"
#import "Macros.h"
#import "TCLYCommunityModel.h"
#import "UIImageView+WebCache.h"
#import "TCLYUser.h"
@interface TCLYEvaluationViewController () <UICollectionViewDataSource, UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bgViewLayoutHeight;
@property (nonatomic, strong) TCLYEvaluationView * evaluationView;

@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UIImageView *goodsImageView;
@property (weak, nonatomic) IBOutlet UILabel *goodsNameLabel;
@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UITextView *evaluationTextView;
@property (strong, nonatomic) UICollectionView *collectionView;
@property (strong, nonatomic) UIButton *uploadImage1;

@property (nonatomic, strong) NSArray<ZLSelectPhotoModel *> *lastSelectMoldels;
@property (nonatomic, strong) NSArray *arrDataSources;
@property (nonatomic, strong) TCLYCommunityModel * communityModel;
@property (nonatomic, strong) TCLYUser * user;

@end

@implementation TCLYEvaluationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = bgColor;
    [self createView];
    [self createTitleView];
    self.user = [[TCLYUser alloc] init];
    self.communityModel = [[TCLYCommunityModel alloc] init];
    
}

-(void)createView{
    NSURL * url = [NSURL URLWithString:self.evaluationDic[@"goods_pic"]];
    [self.goodsImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"LYJia2"]];
    self.goodsNameLabel.numberOfLines = 0;
    self.goodsNameLabel.text = self.evaluationDic[@"goods_name"];
    self.priceLabel.text = self.evaluationDic[@"price"];
    //添加手势
    UITapGestureRecognizer * textFeildTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
    [self.view addGestureRecognizer:textFeildTap];
    
    self.evaluationTextView.editable = YES;
    self.evaluationTextView.font = [UIFont boldSystemFontOfSize:12];
    self.evaluationTextView.returnKeyType = UIReturnKeyDefault;//return键的类型
    self.evaluationTextView.keyboardType = UIKeyboardTypeDefault;//键盘类型
    self.evaluationTextView.textAlignment = NSTextAlignmentLeft; //文本显示的位置默认为居左
    self.evaluationTextView.dataDetectorTypes = UIDataDetectorTypeAll; //显示数据类型的连接模式（如电话号码、网址、地址等）
    self.evaluationTextView.textColor = [UIColor blackColor];
    self.evaluationTextView.text = @"请填写评论";//设置显示的文本内容
    
    //照相机按钮
    self.uploadImage1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.uploadImage1 setImage:[UIImage imageNamed:@"LYevaluationCamera"] forState:UIControlStateNormal];
    [self.uploadImage1 addTarget:self action:@selector(uploadImage1Action:) forControlEvents:UIControlEventTouchUpInside];
    [self.bgView addSubview:self.uploadImage1];
    [self.uploadImage1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.evaluationTextView.mas_bottom).offset(7);
        make.left.mas_equalTo(screenWidth/5/6);
        make.width.mas_equalTo(screenWidth/5);
        make.height.mas_equalTo(screenWidth/5);
    }];
    
    //后面的图片
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(screenWidth/5, screenWidth/5);
    layout.minimumInteritemSpacing = 1;
    layout.minimumLineSpacing = 1.5;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(screenWidth/5+screenWidth/5/3, 270, screenWidth-screenWidth/5-screenWidth/5/3-screenWidth/5/3, screenWidth/5) collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"ZLCollectionCell" bundle:kZLPhotoBrowserBundle] forCellWithReuseIdentifier:@"ZLCollectionCell"];
    [self.bgView addSubview:self.collectionView];
    
    UILabel * logisticsLabel = [[UILabel alloc] init];
    logisticsLabel.text = @"宝贝详情";
    logisticsLabel.font = [UIFont boldSystemFontOfSize:14];
    logisticsLabel.textColor = blackFont;
    logisticsLabel.textAlignment = NSTextAlignmentLeft;
    [self.bgView addSubview:logisticsLabel];
    [logisticsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.uploadImage1.mas_bottom).offset(22);
        make.left.mas_equalTo(12);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(30);
    }];
    
    self.evaluationView = [[TCLYEvaluationView alloc] init];
    [self.bgView addSubview:self.evaluationView];
    [self.evaluationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.uploadImage1.mas_bottom).offset(22);
        make.left.mas_equalTo(logisticsLabel.mas_right).offset(10);
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(100);
    }];
    UIImageView * lineImageView = [[UIImageView alloc] init];
    lineImageView.backgroundColor = [UIColor lightGrayColor];
    [self.bgView addSubview:lineImageView];
    [lineImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(logisticsLabel.mas_bottom).offset(21);
        make.left.mas_equalTo(self.bgView);
        make.right.mas_equalTo(self.bgView);
        make.height.mas_equalTo(1);
    }];
    
    UIButton * submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [submitBtn setTitle:@"提交" forState:UIControlStateNormal];
    [submitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    submitBtn.backgroundColor = btnColor;
    [submitBtn addTarget:self action:@selector(submitBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.bgView addSubview:submitBtn];
    [submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(lineImageView.mas_bottom).offset(0);
        make.right.mas_equalTo(self.bgView);
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(80);
    }];
    
    UIImageView * bgImageView = [[UIImageView alloc] init];
    bgImageView.backgroundColor = bgColor;
    [self.bgView addSubview:bgImageView];
    [bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(submitBtn.mas_bottom).offset(1);
        make.left.mas_equalTo(self.bgView);
        make.right.mas_equalTo(self.bgView);
        make.bottom.mas_equalTo(self.bgView);
    }];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)uploadImage1Action:(id)sender
{
    ZLPhotoActionSheet *actionSheet = [[ZLPhotoActionSheet alloc] init];
    //设置照片最大选择数
    actionSheet.maxSelectCount = 3;
    //设置照片最大预览数
    actionSheet.maxPreviewCount = 20;
    weakify(self);
    [actionSheet showWithSender:self animate:YES lastSelectPhotoModels:self.lastSelectMoldels completion:^(NSArray<UIImage *> * _Nonnull selectPhotos, NSArray<ZLSelectPhotoModel *> * _Nonnull selectPhotoModels) {
        strongify(weakSelf);
        strongSelf.arrDataSources = selectPhotos;
        strongSelf.lastSelectMoldels = selectPhotoModels;
        [strongSelf.collectionView reloadData];
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _arrDataSources.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZLCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZLCollectionCell" forIndexPath:indexPath];
    cell.btnSelect.hidden = YES;
    cell.imageView.image = _arrDataSources[indexPath.row];
    cell.imageView.contentMode = UIViewContentModeScaleAspectFill;

    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZLCollectionCell *cell = (ZLCollectionCell *)[collectionView cellForItemAtIndexPath:indexPath];
    [ZLShowBigImage showBigImage:cell.imageView];
}
//可以上传多张图片
-(void)updataPhotoFor:(NSArray *) imageArray Parameters:(NSDictionary *) parameters
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyyMMddHHmmss";
    NSString *str = [formatter stringFromDate:[NSDate date]];
    NSString *fileName = [NSString stringWithFormat:@"%@openid_avatar.png", str];
    [self.communityModel uploadImageArray:imageArray Parameter:parameters ParamName:@"image" FileName:fileName MimeType:@"image/png" UrlStr:@"http://112.74.114.1/Parenting/index.php/api/order/addcomment_pic"];
}
-(void)submitBtnAction:(id)sender{
    int star = 1;
    UIButton * btn1 = [self.evaluationView viewWithTag:100];
    if (btn1.selected == YES) {
        star = 1;
    }
    UIButton * btn2 = [self.evaluationView viewWithTag:101];
    if (btn2.selected == YES) {
        star = 2;
    }
    UIButton * btn3 = [self.evaluationView viewWithTag:102];
    if (btn3.selected == YES) {
        star = 3;
    }
    UIButton * btn4 = [self.evaluationView viewWithTag:103];
    if (btn4.selected == YES) {
        star = 4;
    }
    UIButton * btn5 = [self.evaluationView viewWithTag:104];
    if (btn5.selected == YES) {
        star = 5;
    }
    NSDictionary * parameters = [NSDictionary dictionaryWithObjectsAndKeys:self.user.userId,@"uid", self.evaluationDic[@"gid"], @"gid", self.evaluationDic[@"oid"], @"oid",[NSNumber numberWithInt:star],@"star", self.evaluationTextView.text, @"content", nil];
    
    NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:self.evaluationDic[@"gid"],@"gid", self.user.userId,@"uid", self.evaluationTextView.text, @"content", nil];
    
    [self updataPhotoFor:_arrDataSources Parameters:dic];
    
    [self.communityModel evaluationSucceedParameter:parameters];
    self.communityModel.isInformation = NO;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"evaluation" object:self];
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden = NO;
    [self.navigationController popToRootViewControllerAnimated:YES];
    

}
-(void)createTitleView{
    self.navigationController.navigationBarHidden = YES;
    UIView * statusView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, screenWidth, 44)];
    statusView.backgroundColor = statusColor;
    [self.view addSubview:statusView];
    
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 20)];
    view.backgroundColor = navColor;
    [self.view addSubview:view];
    
    UILabel * titleLabel = [[UILabel alloc] init];
    titleLabel.text = @"评价";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font = [UIFont boldSystemFontOfSize:20];
    [statusView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(statusView);
        make.centerY.mas_equalTo(statusView);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(30);
    }];
    
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
    leftBtn.tag = 91;
    [leftBtn addTarget:self action:@selector(statusBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [statusView addSubview:leftBtn];
    [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(statusView);
        make.left.mas_equalTo(12);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(20);
    }];
}
-(void)statusBtnAction:(id)sender{
    switch (((UIButton *)sender).tag) {
        case 91:{
            [self.navigationController popToRootViewControllerAnimated:YES];
            self.navigationController.navigationBarHidden = NO;
            self.tabBarController.tabBar.hidden = NO;
        }
            break;
        case 92:{
            
        }
            break;
    }
}
//隐藏键盘的方法
-(void)dismissKeyboard{
    [self.evaluationTextView resignFirstResponder];

}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
